﻿
namespace Patterns4Net.ArchitectureExplorer.Infrastructure
{
    using System.Collections.Generic;

    public class Buffer<T>
    {
        private readonly int size;

        private LinkedList<T> data = new LinkedList<T>();

        public Buffer(int size = 10)
        {
            this.size = size;
        }

        public bool IsEmpty
        {
            get { return this.data.First == null && this.data.Last == null; }
        }

        public T Pop()
        {
            var result = this.data.Last.Value;
            this.data.RemoveLast();
            return result;
        }

        public void Push(T item)
        {
            this.data.AddLast(item);
            if (this.data.Count > this.size)
            {
                this.data.RemoveFirst();
            }
        }

        public void Clear()
        {
            this.data.Clear();
        }
    }
}
